package cn.rokka.rbac.service.impl;


import cn.rokka.rbac.domain.Employee;
import cn.rokka.rbac.mapper.EmployeeMapper;
import cn.rokka.rbac.qo.PageResult;
import cn.rokka.rbac.qo.QueryObject;
import cn.rokka.rbac.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

@Service
@Transactional
public class EmployeeServiceImpl implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public void delete(Long id) {
        employeeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void save(Employee record) {
        employeeMapper.insert(record);
    }

    @Override
    public Employee getById(Long id) {
        return employeeMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Employee> listAll() {
        return employeeMapper.selectAll();
    }

    @Override
    public void update(Employee record) {
        employeeMapper.updateByPrimaryKey(record);
    }

    @Override
    public PageResult<Employee> query(QueryObject qo) {
        int count = employeeMapper.selectForCount(qo);
        if (count==0) {
            return new PageResult<>(qo.getCurrentPage(),qo.getPageSize(),0, Collections.emptyList());
        }else{
            return new PageResult<>(qo.getCurrentPage(),qo.getPageSize(),count,employeeMapper.selectForList(qo));
        }
    }

}
